package com.example.websockettest.controller;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;

import java.awt.*;

/* ━━━━━━佛祖保佑━━━━━━
 *                  ,;,,;
 *                ,;;'(    社
 *      __      ,;;' ' \   会
 *   /'  '\'~~'~' \ /'\.)  主
 * ,;(      )    /  |.     义
 *,;' \    /-.,,(   ) \    码
 *     ) /       ) / )|    农
 *     ||        ||  \)
 *     (_\       (_\
 * ━━━━━━永无BUG━━━━━━
 * @author ：zjq    https://blog.csdn.net/qq_31490071/article/details/51505388
 * @date ：2020/12/9 15:17
 * @description: TODO
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 */
public class HeTest {


    public static void main(String[] args) {
        String hexStr="23 23 32 30 31 38 30 39 30 34";
        String s = hexStr2Str(hexStr);
        System.out.println(s);

        String str="##2018004";

        String hex = HexUtil.encodeHexStr(str, CharsetUtil.CHARSET_UTF_8);

        System.out.println(hex);


        String b = HexUtil.decodeHexStr(hexStr.replaceAll(" ",""));
        System.out.println(b);

        String cc="09 02 00 00 ";
        String s1 = HexUtil.decodeHexStr(cc.replaceAll(" ", ""));
        System.out.println(s1);

        String ddd="303730283039";
        String s1111 = HexUtil.decodeHexStr(ddd.replaceAll(" ", ""));
        System.out.println(s1111);


    }

    public static String hexStr2Str(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;

        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }


}
